<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<%@page import="java.util.ArrayList"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="com.java.FamTree.*" %>

<% 
	//Check to see if we're logged in.
	if (session.getAttribute("UserId") == null) {
	 response.sendRedirect("index.jsp"); // Send them away if they're not logged in.
	}

	Connection con2 = null;
	ArrayList<Event> events = new ArrayList<Event>();
	try {
	    con2 = DriverManager.getConnection("jdbc:mysql://webdev.cs.uwosh.edu:4381/severa24?zeroDateTimeBehavior=convertToNull", "severa24", "d450224");
  
	    /*
	    * Grab all persons by birth date.
	    */
	    PreparedStatement getevents = con2.prepareStatement("SELECT Event.EventId, EventType.EventName AS 'EventType', h.Name AS 'Husband', w.Name AS 'Wife', Event.EventDate, Event.EventPlace, h.PersonId AS 'HId', w.PersonId AS 'WId' FROM Relationship LEFT JOIN Event ON Relationship.EventId = Event.EventId LEFT JOIN EventType ON Event.EventTypeId = EventType.EventTypeId INNER JOIN Person h ON h.PersonId = Relationship.Husband INNER JOIN Person w ON w.PersonId = Relationship.Wife");

	    ResultSet peopleSet = getevents.executeQuery();
	    while(peopleSet.next()) {
	        Event e = new Event();
	        e.EventId = peopleSet.getInt(1);
	        e.EventType = peopleSet.getString(2);
	        e.Husband = peopleSet.getString(3);
	        e.Wife = peopleSet.getString(4);
	        e.EventDate = peopleSet.getString(5);
	        e.EventPlace = peopleSet.getString(6);
	        e.HId = peopleSet.getInt(7);
	        e.WId = peopleSet.getInt(8);
	        events.add(e);
	    }                   
	} catch (SQLException ex) { 
	    System.err.println(ex); 
	} finally {
	    if (con2 != null ) {
	        try {
	            con2.close();
	        } catch (SQLException ex) {
	            System.err.println(ex);
	        }
	    }
	}
%>



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home</title>
</head>
<body>
<Div style="text-align:right;background-color:#F0F0F0;" id="TopMenuBar" >
<%
	out.print("<span id='WelcomSpan'>Welcome " + session.getAttribute("Username") + "  </span>");
	out.print("<a href='index.jsp'><button>Logout</button></a>");
	if((Boolean)session.getAttribute("IsAdmin")){
		out.print("<a href='Admin.jsp'><button>Admin</button></a>");
                out.print("<a href='AddPerson.jsp'><button>Add Person</button></a>");
	}
%>
</Div>
<Div>
 	<link rel="stylesheet" type="text/css" href="Scripts/DataTables-1.9.4/media/css/jquery.dataTables.css">
 	<link rel="stylesheet" type="text/css" href="Scripts/DataTables-1.9.4/media/css/demo_table.css">
	<script type="text/javascript" src="Scripts/jquery-1.8.3.js"></script>
	<script type="text/javascript" src="Scripts/DataTables-1.9.4/media/js/jquery.dataTables.js"></script>
	<script type="text/javascript">
		$(document).ready(function () {
			$("#EventTable").dataTable({"bAutoWidth": false});
		});
	</script>
	<div>
		<div style="width:1000px; margin:0 auto;">
			<br />
			<br />
			<a href='Home.jsp'><button>View People</button></a>
			<br />
			<br />
			<h3>Events</h3>
			<br />
			<table id="EventTable" class="display">
				<thead>
	                <tr>
	                    <th>EventId</th>
	                    <th>Type</th>
	                    <th>Date</th>
	                    <th>Place</th>
	                    <th>Husband</th>
	                    <th>Wife</th>
	                    <% if((Boolean)session.getAttribute("IsAdmin")){ %>
	                    <th>&nbsp;</th>
	                    <% } %>
	                </tr>
                </thead>
                <tbody>
                <%
                    for(int i = 0; i < events.size(); i++) {
                        %>
                        <tr>
                        <form name="MyForm" method=post action="${pageContext.request.contextPath}/RemoveEvent.jsp">
                            <td><% out.print(events.get(i).EventId); %>
                            <input style="display:none" name='EventId'  value='<% out.print(events.get(i).EventId); %>' />
                            </td>
                            <td>
                                <% out.print(events.get(i).EventType); %>
                            </td>
                            <td>
                                <% out.print(events.get(i).EventDate); %>
                            </td>
                            <td>
                                <% out.print(events.get(i).EventPlace); %>
                            </td>
                            <td>
                                <% out.print(events.get(i).Husband); %>
                                <input style="display:none" name='H'  value='<% out.print(events.get(i).HId); %>' />
                            </td>
                            <td>
                                <% out.print(events.get(i).Wife); %>
                                <input style="display:none" name='W'  value='<% out.print(events.get(i).WId); %>' />
                            </td>
                            <% if((Boolean)session.getAttribute("IsAdmin")){ %>
                            <% if( events.get(i).EventType.equals("Marriage") ) { %>
                            <td>
                            	<button class="submit" type="submit" name="sub" value="Remove Event">Unmarry</button>
                            </td>
                            <%
                            } else {
                            %>
                            <td>&nbsp;</td>
                            <%
                            }
                            %>
                            </form>
                             <% } %>
                        </tr>
                        
                    <%
                    }
                %>
                </tbody>
            </table>
		</div>
	</div>
	
</Div>
</body>
</html>


